
using System;
using Mono.Unix.Native;
using Mono.Fuse;
using System.Collections.Generic;
using System.Net.Json;

namespace CouchFS
{


	public class DirectoryHelper
	{

		public static IEnumerable<DirectoryEntry> GetDirectory(string host, string Path)
		{
			
			yield return new DirectoryEntry (".");
			yield return new DirectoryEntry ("..");

			if (Path == "/")
				Path = "";
			String content;
			if (RESTHelper.RequestGet(host, Path, out content))
			{
				JsonTextParser parser = new JsonTextParser();
				JsonObject json = parser.Parse(content);
				JsonObject rows = null;
				foreach (JsonObject field in json as JsonObjectCollection)
                {
					string name = field.Name;
					if (name == "rows")
					{
						rows = field;
						break;
					}
				}
				
				foreach (JsonObject files in rows as JsonArrayCollection)
                {
					foreach (JsonObject field in files as JsonObjectCollection)
					{
	                    string name = field.Name;
	                    
						if (name == "value")
						{
							string value = (string)field.GetValue();
							yield return new DirectoryEntry (value);
						}
					}
				}
			}
		}			
	}
}
